#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include<unordered_map>
using namespace std;

class ResolveHttp
{
private:
public:
    ResolveHttp()
    {
        content_type_map.insert(make_pair("html","text/html"));
        content_type_map.insert(make_pair("png","image/png"));
    }
    void Deserialize(string &request)
    {
        int pos = request.find("\r\n");
        string line = request.substr(0, pos);
        istringstream ss(line);
#pragma region 处理request line
        if (pos != string::npos)
        {
            request.erase(0, pos + 2);
            ss >> method_ >> file_path_ >> version_;
            pos = file_path_.find('.');
            if (pos != string::npos)
                suffix_ = file_path_.substr(pos);
            else
                suffix_ = "/";
            if(suffix_=="/"||suffix_==".html")
            {
                content_type=content_type_map[".html"];
            }
            else
            {
                content_type=content_type_map[suffix_];
            }

        }
#pragma endregion
#pragma region 处理request header
#pragma endregion
    }

    string Serialize(const string& text)
    {
        string response_line="HTTP/1.0 200 OK\r\n";
        string reponse_header="Content-Length: ";
        reponse_header+=to_string(text.size());
        reponse_header+="\r\n";
        reponse_header+="Content-Type: ";
        reponse_header+=content_type;
        reponse_header+="\r\n";
        string blank_line="\r\n";
        string response=response_line;
        response+=reponse_header;
        response+=blank_line;
        response+=text;
        return response;
    }

    void DebugPrint()
    {
        std::cout << " method: " << method_
                  << " filepath: " << file_path_
                  << " version: " << version_
                  << " content-type: "<<content_type
                  << " suffix: " << suffix_ << endl;
    }

public:
    unordered_map<string,string> content_type_map;
    string method_;
    string file_path_;
    string version_;
    string suffix_;
    string content_type;
    /*vector<string> properties;*///未来功能
};